﻿import com.dinmax.flash.draw.control.*
import com.dinmax.flash.draw.common.Point

class Control {
	private var location:Point;
	private var size:Point;
	private var container:MovieClip;
	private var _clip:MovieClip;
	private var selectionEventContainer:Array;
	private var changeEventContainer:Array;
	private var actionEventContainer:Array;
	private var value:Object;
	private var name:String;
	
	private static var s_nInstancia=0;

	function Control(container:MovieClip){
		this.setContainer(this.createMovieClip(container));
		this.selectionEventContainer=new Array();
		this.changeEventContainer=new Array();
		this.actionEventContainer=new Array();
	}
		
	private static function getInstancia():String{
		s_nInstancia++;
		return "mcControl"+s_nInstancia;
	}
	
	private function createMovieClip(clip:MovieClip):MovieClip{
	//trace("Control.as | createMovieClip() | clip= "+clip);
	if(clip==undefined)	clip=container;
	
	var clipe:MovieClip=clip.createEmptyMovieClip(getInstancia(), clip.getNextHighestDepth())
	
	if(this.getClip()==undefined) this.setClip(clipe);
	
	return clipe;
	}
	
	private function removeMovieClip(clip:MovieClip){
		for(var sNested:String in clip){
			if (clip[sNested] instanceof MovieClip)
			this.removeMovieClip(clip[sNested]);
			}
			clip.removeMovieClip();
	}
	
	public function getClip():MovieClip{
		return this._clip;
	}
	public function setClip(clip:MovieClip){
		//trace("Control.as | setClip= "+clip);
		this._clip=clip;
	}

	public function getLocation():Point {
		return location;
	}

	public function setLocation(location:Point):Void {
		this.location = location;
		//trace("Control.as | setLocation() _clip= "+_clip+" container= "+container);
		_clip._x=location.getX();
		_clip._y=location.getY();
		propagateChangeEvent();
	}

	public function getContainer():MovieClip {
		return container;
	};
	
	public function setContainer(container:MovieClip):Void {
		this.container = container;
	};

	public function getSize():Point {
		return size;
	}

	public function setSize(size:Point):Void {
		this.size = size;
		propagateChangeEvent();
	}

	public function reDraw(){
		
	}
	
	public function setValue(value:Object) {
		this.value=value;
	}

	public function getValue():Object {
		return this.value;
	}
	
	public function getName():String {
		return this.name;
	}
	public function setName(name:String){
		this.name=name;
	}

	public function toString():String {
		return String(container);
	}
	
	public function addSelectionEvent(event:SelectionEvent) {
		selectionEventContainer.push(event);
	}
	
	public function propagateSelectionEvent() {
		for (var i : Number = 0; i < selectionEventContainer.length; i++) {
			var se:SelectionEvent =selectionEventContainer[i];
			if(se instanceof SelectionEventExtended){
				SelectionEventExtended(se).selectedExtended(this);
			} else {
			se.selected();
			}
		}
	}

	public function addChangeEvent(event:ChangeEvent) {
		//trace("Control.as | addChangeEvent()");
		changeEventContainer.push(event);
	}

	public function propagateChangeEvent() {
	for (var i : Number = 0; i < changeEventContainer.length; i++) {
			var ce:ChangeEvent =changeEventContainer[i];
			if(ce instanceof ChangeEventExtended){
				ChangeEventExtended(ce).changedExtended(this);
			} else {
				ce.changed();
			}
		}
	}
	
	public function addActionEvent(event:ActionEvent){
		actionEventContainer.push(event);
	}

	public function propagateActionEvent(key:String) {
		for (var i : Number = 0; i < actionEventContainer.length; i++) {
			var ae:ActionEvent =actionEventContainer[i];
			if(ae instanceof ActionEventExtended){
				ActionEventExtended(ae).actionExtended(key);
			} else {
			ae.action();
			}
		}
	}

}